package cn.yangliu.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;

/**
 * 作者 杨柳
 * 创建时间 2017-08-24 13:01
 */
@Configuration
public class RabbitMqConfig {
    @Bean
    public Queue testQueue() {
        return new Queue("test");
    }


    //===============以下是验证topic Exchange的队列==========
    @Bean
    public Queue topicMessage() {
        return new Queue("topic.message");
    }
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }
    /**
     * 将队列topic.message与exchange绑定，binding_key为topic.message,就是完全匹配
     */
    @Bean
    Binding bindingTopicExchange(Queue topicMessage, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicMessage).to(topicExchange).with("topic.message");
    }

    //===============以下是验证Fanout Exchange的队列==========
    @Bean
    public Queue fanoutMessage() {
        return new Queue("fanout");
    }

    @Bean
    public Queue fanoutMessageA() {
        return new Queue("fanoutMessageA");
    }
    @Bean
    public Queue fanoutMessageB() {
        return new Queue("fanoutMessageB");
    }
    @Bean
    public Queue fanoutMessageC() {
        return new Queue("fanoutMessageC");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingFanoutExchangeA(Queue fanoutMessageA,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageA).to(fanoutExchange);
    }

    @Bean
    Binding bindingFanoutExchangeB(Queue fanoutMessageB,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageB).to(fanoutExchange);
    }

    @Bean
    Binding bindingFanoutExchangeC(Queue fanoutMessageC,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessageC).to(fanoutExchange);
    }

    @Bean
    Binding bindingFanoutExchange(Queue fanoutMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutMessage).to(fanoutExchange);
    }

    @Bean
    public Queue fanoutEntityMessage() {
        return new Queue("fanoutEntity");
    }

    @Bean
    FanoutExchange fanoutEntityExchange() {
        return new FanoutExchange("fanoutEntityExchange");
    }

    @Bean
    Binding bindingFanoutEntityExchange(Queue fanoutEntityMessage,FanoutExchange fanoutEntityExchange) {
        return BindingBuilder.bind(fanoutEntityMessage).to(fanoutEntityExchange);
    }

    @Resource
    private CachingConnectionFactory cachingConnectionFactory;


    /**
     * 因为要设置回调类，所以应是prototype类型，如果是singleton类型，则回调类为最后一次设置
     */
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplatenew() {
        return new RabbitTemplate(cachingConnectionFactory);
    }
}
